package com.indusfo.spc.service.impl;

import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.Device;
import com.indusfo.spc.pojo.Maintain;
import com.indusfo.spc.pojo.MaintainPlan;
import com.indusfo.spc.pojo.MaintainPlanDetail;
import com.indusfo.spc.service.MaintainPlanService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MaintainPlanServiceImpl
 * @Description TODO
 * @Author louk
 * @Date 2019/12/11 14:56
 * @Version 1.0
 */
@Service
public class MaintainPlanServiceImpl implements MaintainPlanService {

    @Resource
    MaintainPlanMapper maintainPlanMapper;
    @Resource
    MaintainPlanDetailMapper maintainPlanDetailMapper;
    @Resource
    DeviceTypeMaintainMapper deviceTypeMaintainMapper;
    @Resource
    MaintainMapper maintainMapper;
    /**
     * 新增设备保养计划
     * @param maintainPlan
     * @return
     */
    @Override
    @Transactional
    public JSONObject insertMaintainPlan(MaintainPlan maintainPlan,Integer[] maintainIds) {
        String checkResult = checkParam(maintainPlan);
        if(!StringUtils.isEmpty(checkResult)){
            return JSONObject.build(JSONObject.ERROR,checkResult);
        }
        int insertCount = maintainPlanMapper.insertMaintainPlan(maintainPlan);
        if(insertCount>0){
            /**
             * 当新增成功一个设备保养计划默认将器所属的设备的设备类型都添加到设备保养明细表里
             */
            //设备id
            Integer deviceId = maintainPlan.getDeviceId();
            //获取到该设备类型下的保养项目id集合
//            List<Integer> maintainIdList=deviceTypeMaintainMapper.queryByDeviceId(deviceId);
            if(maintainIds!=null && maintainIds.length!=0){
                    int updateCount = maintainPlanDetailMapper.updateMaintainPlanDetailList(maintainPlan.getMaintainPlanId(), maintainIds);
                    if(updateCount>0){
                        return JSONObject.build(JSONObject.SUCCESS,"新增成功");
                    }
                return JSONObject.build(JSONObject.ERROR,"新增设备保养明细失败");
            }
            return JSONObject.build(JSONObject.SUCCESS,"新增成功");
        }
        return JSONObject.build(JSONObject.ERROR,"新增失败");
    }

    /**
     * 查询设备保养明细
     * @param maintainPlanDetail
     * @return
     */
    @Override
    public JSONObject queryMaintainPlanDetail(MaintainPlanDetail maintainPlanDetail) {
        Integer pagesize = maintainPlanDetail.getPagesize();
        Integer pageindex = maintainPlanDetail.getPageindex();
        if(pagesize != null && pageindex != null) {
            maintainPlanDetail.setIncept(pagesize*(pageindex - 1));
        }
        Integer maintainPlanId = maintainPlanDetail.getMaintainPlanId();
        if(maintainPlanId==null || "".equals(maintainPlanId)){
            return JSONObject.build(JSONObject.SUCCESS,"暂无数据",null);
        }
        List<MaintainPlanDetail> maintainPlanDetailList = maintainPlanDetailMapper.queryMaintainPlanDetail(maintainPlanDetail);
        if(!maintainPlanDetailList.isEmpty()){
            int count = maintainPlanDetailMapper.countMaintainPlanDetail(maintainPlanDetail);
            return new JSONObject("查询成功",maintainPlanDetailList,count);
        }
        return new JSONObject("暂无数据",maintainPlanDetailList,null);
    }

    /**
     * 编辑设备保养计划
     * @param maintainPlan
     * @return
     */
    @Override
    @Transactional
    public JSONObject updateMaintainPlan(MaintainPlan maintainPlan, Integer[] maintainIds) {
        Boolean flag=false;
        if (maintainPlan.getMaintainPlanId()==null || "".equals(maintainPlan.getMaintainPlanId())){
            return JSONObject.build(JSONObject.ERROR,"请选择设备保养计划");
        }
        if(maintainPlan.getAuditState()!=null && maintainPlan.getAuditState()==2){
            return JSONObject.build(JSONObject.ERROR,"审核过的不能编辑");
        }
        String checkResult = checkParam(maintainPlan);
        if(!StringUtils.isEmpty(checkResult)){
            return JSONObject.build(JSONObject.ERROR,checkResult);
        }
        //查询当前设备保养计划的明细保养项目
        List<Integer> inspectIdList = maintainPlanDetailMapper.queryByMaintainPlan(maintainPlan.getMaintainPlanId());
        List<Integer> insertList=new ArrayList<>();
        List<Integer> deleteList=new ArrayList<>();
        //所传递的数组当中不存在数据库查询的集合说明删除了,数据库查询的集合中不存在传递的数组说明是新增
        if(maintainIds!=null && maintainIds.length!=0 && !inspectIdList.isEmpty()){
            List<Integer> tempMaintainIds=new ArrayList<>();
            for (Integer id : maintainIds) {
                tempMaintainIds.add(id);
            }
            for (Integer inspectId : maintainIds) {
                //不包含说明是要进行新增的
                if(!inspectIdList.contains(inspectId)){
                    insertList.add(inspectId);
                }
            }
            //传递过来的数组不包含当前数据库查询出来的则为要进行删除的
            for (Integer inspectId : inspectIdList) {
                if(!tempMaintainIds.contains(inspectId)){
                    deleteList.add(inspectId);
                }
            }
            if(!insertList.isEmpty()){
                maintainPlanDetailMapper.updateMaintainPlanDetailList(maintainPlan.getMaintainPlanId(),insertList.toArray(new Integer[insertList.size()]));

            }
            if(!deleteList.isEmpty()){
                maintainPlanDetailMapper.defineMaintainPlanDetail(maintainPlan.getMaintainPlanId(),deleteList.toArray(new Integer[deleteList.size()]),2);
            }
        }else if((maintainIds==null || maintainIds.length==0) && !inspectIdList.isEmpty()){
            //如果传递的数据为空,而数据库数据存在则删除数据库中的数据
            maintainPlanDetailMapper.defineMaintainPlanDetail(maintainPlan.getMaintainPlanId(),inspectIdList.toArray(new Integer[inspectIdList.size()]),2);
        }else if((maintainIds!=null && maintainIds.length!=0) && inspectIdList.isEmpty()){
            //如果传递的数据不为空,而数据库不存在数据则进行新增
            maintainPlanDetailMapper.updateMaintainPlanDetailList(maintainPlan.getMaintainPlanId(),maintainIds);
        }
        int updateMaintainPlan = maintainPlanMapper.updateMaintainPlan(maintainPlan);
        if(updateMaintainPlan>0){
            return JSONObject.build(JSONObject.SUCCESS,"编辑成功");
        }
        return JSONObject.build(JSONObject.ERROR,"编辑失败");

    }
    /**
     * 审核状态修改
     * @param maintainPlanIds
     * @param auditState
     * @return
     */
    @Override
    @Transactional
    public JSONObject auditStateChange(Integer[] maintainPlanIds, Integer auditState,Integer[] auditStateIds) {
        if(maintainPlanIds==null || maintainPlanIds.length==0){
            return JSONObject.build(JSONObject.ERROR,"请选择设备保养计划单号");
        }
        if(auditState==null || "".equals(auditState)){
            return JSONObject.build(JSONObject.ERROR,"审核状态未传递");
        }
        if(auditStateIds==null || auditStateIds.length==0 ){
            return JSONObject.build(JSONObject.ERROR,"未获取到当前信息的审核状态");
        }
        for (Integer auditStateId : auditStateIds) {
            if(auditState==auditStateId){
                if(auditState==1){
                    return JSONObject.build(JSONObject.ERROR,"未审核的不能反审核");
                }else if(auditState==2){
                    return JSONObject.build(JSONObject.ERROR,"已审核,不能再审核");
                }

            }
        }
        int auditStateChangeCount = maintainPlanMapper.auditStateChange(maintainPlanIds, auditState);
        if(auditStateChangeCount>0){

            return JSONObject.build(JSONObject.SUCCESS,"审核状态修改成功");
        }
        return JSONObject.build(JSONObject.ERROR,"审核状态修改失败");
    }

    /**
     * 查询设备保养项目
     * @param maintainPlan
     * @return
     */
    @Override
    public JSONObject queryMaintainPlan(MaintainPlan maintainPlan,String startDate,String endDate) {
        Integer pagesize = maintainPlan.getPagesize();
        Integer pageindex = maintainPlan.getPageindex();
        if(pagesize != null && pageindex != null) {
            maintainPlan.setIncept(pagesize*(pageindex - 1));
        }
        if(!StringUtils.isEmpty(startDate)){
            try {
                Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDate);
            } catch (ParseException e) {
                return JSONObject.build(JSONObject.ERROR,"开始日期格式错误");
            }
        }
        if(!StringUtils.isEmpty(endDate)){
            try {
                Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
            } catch (ParseException e) {
                return JSONObject.build(JSONObject.ERROR,"结束日期格式错误");
            }
        }
        List<MaintainPlan> maintainPlanList = maintainPlanMapper.queryMaintainPlanList(maintainPlan,startDate,endDate);
        if(!maintainPlanList.isEmpty()){
            int count = maintainPlanMapper.countMaintainPlanList(maintainPlan,startDate,endDate);
            return JSONObject.oK("查询成功",maintainPlanList,count);
        }
        return JSONObject.oK("没有相关数据",maintainPlanList,null);
    }
    /**
     * 数据状态修改
     * @param maintainPlanIds
     * @param dataState
     * @return
     */
    @Override
    @Transactional
    public JSONObject defineDataState(Integer[] maintainPlanIds,Integer dataState,Integer[] auditState) {
        if(maintainPlanIds.length==0 || maintainPlanIds==null){
            return JSONObject.build(JSONObject.ERROR,"请选择设备保养项目");
        }
        if(auditState==null || auditState.length==0){
            return JSONObject.build(JSONObject.ERROR,"未获取到审核状态");
        }
        for (Integer as : auditState) {
            if(as==2){
                return JSONObject.build(JSONObject.ERROR,"审核过的不能删除");
            }
        }

        if(dataState==null || "".equals(dataState)){
            return JSONObject.build(JSONObject.ERROR,"未传递数据状态");
        }
        //判断当前设备保养计划是否被设备保养作业所用
        int maintainWorkCount = maintainPlanMapper.countMaintainWork(maintainPlanIds);
        if(maintainWorkCount>0){
            return JSONObject.build(JSONObject.ERROR,"设备保养作业关联所选设备保养计划,删除失败");
        }
        int defineCount = maintainPlanMapper.defineDataState(maintainPlanIds, dataState);
        int defineByMaintainPlanIdsCount = maintainPlanDetailMapper.defineByMaintainPlanIds(maintainPlanIds, dataState);
        if(defineCount>0){
            return JSONObject.build(JSONObject.SUCCESS,"删除成功");
        }
        return JSONObject.build(JSONObject.SUCCESS,"删除失败");
    }

    @Override
    public JSONObject filterMaintainList(MaintainPlan maintainPlan,Integer[] maintainIds) {
        List<Maintain> maintainList=new ArrayList<>();
        Integer pagesize = maintainPlan.getPagesize();
        Integer pageindex = maintainPlan.getPageindex();
        Integer incept=null;
        if(pagesize != null && pageindex != null) {
            maintainPlan.setIncept(pagesize*(pageindex - 1));
            incept=pagesize*(pageindex - 1);
        }
        if(maintainIds==null || "".equals(maintainIds) || maintainIds.length==0) {
            maintainIds = null;
        }
        maintainList = maintainMapper.filterMaintainListByMaintainIds(pagesize,incept,maintainIds);

        if(!maintainList.isEmpty()){
            int  count = maintainMapper.countFilterMaintainListByMaintainIds(maintainIds);
            return JSONObject.oK(maintainList,count);
        }
        return JSONObject.oK(maintainList,null);
    }

    @Override
    public JSONObject queryMaintainByDevice(Integer deviceId) {
        if(deviceId!=null && !"".equals(deviceId)){
            List<Maintain> maintainList = maintainMapper.queryByDevice(deviceId);
            if(!maintainList.isEmpty()){
                return JSONObject.oK("查询成功",maintainList,maintainList.size());
            }
        }

        return JSONObject.oK("没有相关数据",null);
    }


    public String checkParam(MaintainPlan maintainPlan){
        //设备保养项目编号
        String maintainPlanNo = maintainPlan.getMaintainPlanNo();
        //设备id
        Integer deviceId = maintainPlan.getDeviceId();
        //制单人
        Integer userId = maintainPlan.getUserId();
        Integer auditState = maintainPlan.getAuditState();
        String remark = maintainPlan.getRemark();
        if(StringUtils.isEmpty(maintainPlanNo)){
            return "设备保养项目编号为空";
        }
        int noCount = maintainPlanMapper.queryPlanNo(maintainPlan);
        if(noCount>0){
            return "设备保养项目编号已存在";
        }
        if(deviceId==null || "".equals(deviceId)){
            return "未选择设备";
        }
        if(userId==null || "".equals(userId)){
            return "制单人未传递";
        }
        if(auditState==null || "".equals(auditState)){
            return "审核状态未传递";
        }
        if(StringUtils.isEmpty(remark) && remark.length()>100){
            return "备注内容过长,长度100";
        }
        return null;
    }

    public static void main(String[] args) {
        
    }
}
